package service;

import dao.memberDao;

import java.util.*;

public class memberService {
    public memberDao dao = new memberDao();
    Scanner sc = new Scanner(System.in);

    public void memberServiceUI() {
        boolean flag = true;
        while (flag) {
            System.out.println("--------会员管理系统--------");
            System.out.println("|\t1. 注册会员\t\t");
            System.out.println("|\t2. 会员查询\t\t");
            System.out.println("|\t3. 余额充值\t\t");
            System.out.println("|\t4. 查询积分\t\t");
            System.out.println("|\t5. 矫正积分\t\t");
            System.out.println("|\t6. 积分兑换\t\t");
            System.out.println("|\t7. 注销会员\t\t");
            System.out.println("|\t8. 退出会员系统\t\t");
            System.out.println("-------------------------");
            int chooseNum = sc.nextInt();
            switch (chooseNum) {
                case 1:
                    System.out.println("输入会员名");
                    String newname = sc.next();
                    System.out.println("输入会员账号");
                    int newnum = sc.nextInt();
                    dao.register(newname, newnum);
                    break;
                case 2:
                    System.out.println("输入查询方式：1,会员号 2,会员名");
                    if (sc.nextInt() == 1) {
                        System.out.println("输入卡号");
                        int id = sc.nextInt();
                        if (dao.haveMember(id)) {
                            System.out.println("查无此人");
                        }
                    } else if (sc.nextInt() == 2) {
                        System.out.println("输入名字");
                        String uname = sc.next();
                        if (dao.haveMember(uname)) {
                            System.out.println("查无此人");
                        }
                    } else {
                        System.out.println("你丫倒是写个正常的数字");
                    }
                    break;
                case 3:
                    System.out.println("输入查询方式：1,会员号 2,会员名");
                    if (sc.nextInt() == 1) {
                        System.out.println("输入卡号");
                        int newid = sc.nextInt();
                        System.out.println("输入充值金额");
                        int newmoney = sc.nextInt();
                        if (!dao.charge(newid, newmoney)) {
                            System.out.println("卡号错误");
                        }
                    } else if (sc.nextInt() == 2) {
                        System.out.println("输入名字");
                        String uname = sc.next();
                        System.out.println("输入充值金额");
                        int newmoney = sc.nextInt();
                        if (!dao.charge(uname, newmoney)) {
                            System.out.println("卡号错误");
                        }
                    } else {
                        System.out.println("你丫倒是写个正常的数字");
                    }
                    break;
                case 4:
                    System.out.println("输入查询方式：1,会员号 2,会员名");
                    int choose = sc.nextInt();
                    if (choose == 1) {
                        System.out.println("输入卡号");
                        int uid = sc.nextInt();
                        if (dao.getMember(uid).getMemberId() != uid) {
                            System.out.println("卡号错误");
                        }else {System.out.println(dao.getIntegral(uid));}
                    } else if (choose == 2) {
                        System.out.println("输入名字");
                        String uname = sc.next();
                        if (!Objects.equals(dao.getMember(uname).getMemberName(), uname)) {
                            System.out.println("会员名错误");
                        }else {System.out.println(dao.getIntegral(uname));}
                    } else {
                        System.out.println("你丫倒是写个正常的数字");
                    }
                    break;
                case 5:
                    System.out.println("输入卡号");
                    int uid = sc.nextInt();
                    System.out.println("输入正确积分");
                    int rightIntegral = sc.nextInt();
                    if(dao.setIntegral(uid,rightIntegral)){
                        System.out.println("修改成功");
                    }else {
                        System.out.println("卡号错误");
                    }
                    break;
                case 6:
                    integralExchange();
                    break;
                case 7:
                    System.out.println("输入卡号");
                    int delId = sc.nextInt();
                    if(dao.deleteMember(delId)){
                        System.out.println("删除成功");
                    }else {
                        System.out.println("卡号错误");
                    }
                    break;
                case 8:
                    flag = false;
                    break;
                default:
                    System.out.println("请输入数字1-5");
            }
        }
    }

    public void integralExchange() {
        Map<String, Integer> map = new HashMap<>();
        map.put("大大抱枕", 18);
        map.put("大大黄博抱枕", 180);
        map.put("大大李雨阳抱枕", 1800);
        Map<Character, Boolean> YN = new HashMap<>();
        YN.put('Y', true);
        YN.put('N', false);

        boolean Flag = true;
        while(Flag) {
            Set<Map.Entry<String, Integer>> set = map.entrySet();
            System.out.println("可兑换商品:");
            for (Map.Entry<String, Integer> entry : set) {
                System.out.println(STR."商品:\{entry.getKey()}需要积分\t\{entry.getValue()}");
            }
            System.out.println("请输入兑换商品名称");
            String sth = sc.next();
            System.out.println("请输入会员号");
            int memberNum = sc.nextInt();
            System.out.println("请输入兑换数量");
            int sthNum = sc.nextInt();
            if(dao.getMember(memberNum).getMemberIntegral() - map.get(sth) * sthNum >= 0){
                dao.setIntegral(memberNum,dao.getMember(memberNum).getMemberIntegral() - map.get(sth) * sthNum);
                System.out.println("兑换成功");
                System.out.println(STR."剩余积分:\{dao.getIntegral(memberNum)}");
            } else {
                System.out.println("余额不足");
            }
            System.out.println("是否继续兑换 Y/N");
            Character choose = sc.next().charAt(0);
            Flag = YN.get(choose);
        }
    }
}
